home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_534 / term / source.lzh / Identify.c < prev    next >
C/C++ Source or Header  |  1991-07-06  |  9KB  |  437 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: Identify.c
  6.  *    Created ..: Thursday 02-May-91 15:00
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    02-May-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "TermGlobal.h"
  16.  
  17.     /* The file types we know. */
  18.  
  19. enum    {    TYPE_DIR,TYPE_FILE,TYPE_ICON,
  20.         TYPE_TEXT,
  21.         TYPE_C,TYPE_H,
  22.         TYPE_ASM,TYPE_I,
  23.         TYPE_MOD,
  24.         TYPE_REXX,
  25.         TYPE_BASIC,
  26.         TYPE_TEX,TYPE_METAFONT,TYPE_GF,TYPE_TEXFONT,TYPE_TEXDVI,TYPE_FLIB,
  27.         TYPE_OLDMANX,TYPE_NEWMANX,TYPE_OLDMANXLIB,TYPE_NEWMANXLIB,
  28.         TYPE_OBJECT,TYPE_LIB,
  29.         TYPE_EXECUTABLE,
  30.         TYPE_LIBRARY,TYPE_DEVICE,TYPE_FILESYS,TYPE_HANDLER,
  31.         TYPE_GIF,TYPE_ILBM,TYPE_ANIM,TYPE_8SVX,TYPE_SMUS,TYPE_FTXT,TYPE_PREFS,TYPE_TERM,
  32.         TYPE_IMPLODER,TYPE_POWERPACKER,
  33.         TYPE_ARC,TYPE_LHARC,TYPE_ZOO,TYPE_ZIP,TYPE_DMS,TYPE_WARP,TYPE_ZOOM,
  34.     };
  35.  
  36.     /* A structure containing both a file name suffix and the
  37.      * approriate file type.
  38.      */
  39.  
  40. struct Suffix
  41. {
  42.     UBYTE    *Name;
  43.     UBYTE     Type;
  44. };
  45.  
  46.     /* A table of valid ASCII characters (7 bits). */
  47.  
  48. STATIC BYTE ID_ValidTab[256] =
  49. {
  50.     0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,
  51.     0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,
  52.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  53.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  54.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  55.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  56.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  57.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  58.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  59.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  60.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  61.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  62.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  63.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  64.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  65.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  66. };
  67.  
  68.     /* A table of clearly invalid ASCII characters (8 bits). */
  69.  
  70. STATIC BYTE ID_InvalidTab[256] =
  71. {
  72.     1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,
  73.     1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1,
  74.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  75.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  76.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  77.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  78.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  79.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  80.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  81.     1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,
  82.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  83.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  84.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  85.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  86.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  87.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  88. };
  89.  
  90.     /* Some file name suffixes for text files and the approriate
  91.      * file types.
  92.      */
  93.  
  94. STATIC struct Suffix TextSuffix[] =
  95. {
  96.     ".C",    TYPE_C,
  97.     ".CP",    TYPE_C,
  98.     ".CC",    TYPE_C,
  99.     ".H",    TYPE_H,
  100.     ".ASM",    TYPE_ASM,
  101.     ".A",    TYPE_ASM,
  102.     ".S",    TYPE_ASM,
  103.     ".I",    TYPE_I,
  104.     ".BAS",    TYPE_BASIC,
  105.     ".GFA",    TYPE_BASIC,
  106.     ".REXX",TYPE_REXX,
  107.     ".CED",    TYPE_REXX,
  108.     ".VLT",    TYPE_REXX,
  109.     ".CPR",    TYPE_REXX,
  110.     ".TxEd",TYPE_REXX,
  111.     ".TEX",    TYPE_TEX,
  112.     ".STY",    TYPE_TEX,
  113.     ".MF",    TYPE_METAFONT,
  114.     ".MOD",    TYPE_MOD,
  115.     ".DEF",    TYPE_MOD
  116. };
  117.  
  118.     /* Some more file name suffixes for executable files and the
  119.      * approriate file types.
  120.      */
  121.  
  122. STATIC struct Suffix ExecutableSuffix[] =
  123. {
  124.     ".device",    TYPE_DEVICE,
  125.     ".library",    TYPE_LIBRARY,
  126.     "FileSystem",    TYPE_FILESYS,
  127.     "Handler",    TYPE_HANDLER
  128. };
  129.  
  130.     /* This list contains the names of all icon files which can be
  131.      * attached to the files to be identified. The flag byte indicates
  132.      * whether an icon is available or not.
  133.      */
  134.  
  135. STATIC UBYTE *FileTypes[] =
  136. {
  137.     NULL,
  138.     NULL,
  139.     "Workbench icon file",
  140.     "ASCII text file",
  141.     "`C' source code file",
  142.     "`C' header file",
  143.     "Assembly language source code file",
  144.     "Assembly language include file",
  145.     "Modula-2/Oberon source code file",
  146.     "ARexx script file",
  147.     "Basic source code",
  148.     "TeX document file",
  149.     "Metafont font file",
  150.     "Generic font file",
  151.     "Packed TeX font file",
  152.     "TeX device independent output file",
  153.     "PasTeX font library file",
  154.     "Manx 3.x object code file",
  155.     "Manx 5.x object code file",
  156.     "Manx 3.x library file",
  157.     "Manx 5.x library file",
  158.     "AmigaDOS object code file",
  159.     "AmigaDOS object code library file",
  160.     "AmigaDOS executable file",
  161.     "System library file",
  162.     "System device file",
  163.     "AmigaDOS filing system handler file",
  164.     "AmigaDOS handler file",
  165.     "GIF image file",
  166.     "IFF-ILBM image file",
  167.     "IFF-ANIM animation file",
  168.     "IFF-8SVX sound file",
  169.     "IFF-SMUS score file",
  170.     "IFF-FTXT text file",
  171.     "IFF-PREF preferences file",
  172.     "IFF-TERM `term' configuration file",
  173.     "Imploder data output file",
  174.     "PowerPacker data output file",
  175.     "Arc archive file",
  176.     "LhArc archive file",
  177.     "Zoo archive file",
  178.     "PKZip archive file",
  179.     "DMS disk archive file",
  180.     "Warp disk archive file",
  181.     "Zoom disk archive file"
  182. };
  183.  
  184.     /* Identify(UBYTE *Name):
  185.      *
  186.      *    Simple routine to identify a file type by looking at
  187.      *    its first 400 bytes. If successful a comment is
  188.      *    attached to the file describing the file type.
  189.      */
  190.  
  191. VOID
  192. Identify(UBYTE *Name)
  193. {
  194.     ULONG    *Buffer;
  195.     UBYTE     Type = TYPE_FILE;
  196.     SHORT     i,Len = strlen(Name);
  197.  
  198.         /* Allocate a buffer for the first 400 bytes of the
  199.          * file.
  200.          */
  201.  
  202.     if(Buffer = (ULONG *)AllocVec(400,MEMF_PUBLIC))
  203.     {
  204.         BPTR File,Size;
  205.  
  206.             /* Open the file. */
  207.  
  208.         if(File = Open(Name,MODE_OLDFILE))
  209.         {
  210.                 /* Read the first 400 bytes. */
  211.  
  212.             if((Size = Read(File,Buffer,400)) >= sizeof(ULONG))
  213.             {
  214.                     /* Examine the first longword. */
  215.  
  216.                 switch(Buffer[0])
  217.                 {
  218.                     case 0x03E7:    Type = TYPE_OBJECT;
  219.                             break;
  220.  
  221.                     case 0x03F3:    Type = TYPE_EXECUTABLE;
  222.                             break;
  223.  
  224.                     case 0x03FA:    Type = TYPE_LIB;
  225.                             break;
  226.  
  227.                     case 0xF7593647:Type = TYPE_TEXFONT;
  228.                             break;
  229.  
  230.                     case 0xF7020183:Type = TYPE_TEXDVI;
  231.                             break;
  232.  
  233.                     case 0xF7832020:Type = TYPE_GF;
  234.                             break;
  235.  
  236.                     case 0x504B0304:Type = TYPE_ZIP;
  237.                             break;
  238.  
  239.                     default:    break;
  240.                 }
  241.  
  242.                     /* No match yet, see if it's an
  243.                      * ASCII file.
  244.                      */
  245.  
  246.                 if(Type == TYPE_FILE)
  247.                 {
  248.                     UBYTE    *CharBuffer = (UBYTE *)Buffer;
  249.                     SHORT     Count = 0;
  250.  
  251.                     for(i = 0 ; i < Size ; i++)
  252.                     {
  253.                         if(ID_ValidTab[CharBuffer[i]])
  254.                             Count++;
  255.                         else
  256.                         {
  257.                             if(ID_InvalidTab[CharBuffer[i]])
  258.                             {
  259.                                 Count = 0;
  260.  
  261.                                 break;
  262.                             }
  263.                         }
  264.                     }
  265.  
  266.                         /* If more than 75% of the
  267.                          * characters in the first
  268.                          * 400 bytes are legal
  269.                          * ASCII characters this
  270.                          * file is supposed to be
  271.                          * a text file.
  272.                          */
  273.  
  274.                     if(Count > 3 * (Size / 4))
  275.                         Type = TYPE_TEXT;
  276.                 }
  277.  
  278.                     /* Still no match, have another try... */
  279.  
  280.                 if(Type == TYPE_FILE)
  281.                 {
  282.                     if((Buffer[0] & 0xFFFF0000) == 0x1A080000)
  283.                         Type = TYPE_ARC;
  284.                     else
  285.                     {
  286.                         if((Buffer[0] & 0x0000FFFF) == 0x00002D6C && (Buffer[1] & 0xFF00FF00) == 0x68002D00)
  287.                             Type = TYPE_LHARC;
  288.                         else
  289.                         {
  290.                             switch(Buffer[0] & 0xFFFF0000)
  291.                             {
  292.                                 case 0x434A0000:    Type = TYPE_NEWMANX;
  293.                                             break;
  294.  
  295.                                 case 0x414A0000:    Type = TYPE_OLDMANX;
  296.                                             break;
  297.  
  298.                                 case 0x636A0000:    Type = TYPE_NEWMANXLIB;
  299.                                             break;
  300.  
  301.                                 case 0x616A0000:    Type = TYPE_OLDMANXLIB;
  302.                                             break;
  303.  
  304.                                 case 0xF5000000:    Type = TYPE_BASIC;
  305.                                             break;
  306.  
  307.                                 case 0xE3100000:    Type = TYPE_ICON;
  308.                                             break;
  309.  
  310.                                 default:        break;
  311.                             }
  312.                         }
  313.                     }
  314.                 }
  315.  
  316.                 if(Type == TYPE_FILE)
  317.                 {
  318.                     switch(Buffer[0])
  319.                     {
  320.                         case 'FLIB':    Type = TYPE_FLIB;
  321.                                 break;
  322.  
  323.                         case 'FORM':    switch(Buffer[2])
  324.                                 {
  325.                                     case 'ILBM':    Type = TYPE_ILBM;
  326.                                             break;
  327.  
  328.                                     case 'ANIM':    Type = TYPE_ANIM;
  329.                                             break;
  330.  
  331.                                     case '8SVX':    Type = TYPE_8SVX;
  332.                                             break;
  333.  
  334.                                     case 'SMUS':    Type = TYPE_SMUS;
  335.                                             break;
  336.  
  337.                                     case 'FTXT':    Type = TYPE_FTXT;
  338.                                             break;
  339.  
  340.                                     case 'PREF':    Type = TYPE_PREFS;
  341.                                             break;
  342.  
  343.                                     case 'TERM':    Type = TYPE_TERM;
  344.                                             break;
  345.                                 }
  346.  
  347.                                 break;
  348.  
  349.                         case 'IMP!':    Type = TYPE_IMPLODER;
  350.                                 break;
  351.  
  352.                         case 'PP20':    Type = TYPE_POWERPACKER;
  353.                                 break;
  354.  
  355.                         case 'DMS!':    Type = TYPE_DMS;
  356.                                 break;
  357.  
  358.                         case 'Warp':    Type = TYPE_WARP;
  359.                                 break;
  360.  
  361.                         case 'ZOOM':    Type = TYPE_ZOOM;
  362.                                 break;
  363.  
  364.                         case 'ZOO ':    Type = TYPE_ZOO;
  365.                                 break;
  366.  
  367.                         case 'GIF8':    Type = TYPE_GIF;
  368.                                 break;
  369.                     }
  370.                 }
  371.  
  372.                     /* Take a look at the file name
  373.                      * suffixes.
  374.                      */
  375.  
  376.                 switch(Type)
  377.                 {
  378.                     case TYPE_TEXT:
  379.                             for(i = 0 ; i < sizeof(TextSuffix) / sizeof(struct Suffix) ; i++)
  380.                             {
  381.                                 Size = strlen(TextSuffix[i] . Name);
  382.  
  383.                                 if(Len >= Size)
  384.                                 {
  385.                                     if(!StrCmp(&Name[Len - Size],TextSuffix[i] . Name))
  386.                                     {
  387.                                         Type = TextSuffix[i] . Type;
  388.                                         break;
  389.                                     }
  390.                                 }
  391.                             }
  392.  
  393.                             break;
  394.  
  395.                     case TYPE_EXECUTABLE:
  396.                             for(i = 0 ; i < sizeof(ExecutableSuffix) / sizeof(struct Suffix) ; i++)
  397.                             {
  398.                                 Size = strlen(ExecutableSuffix[i] . Name);
  399.  
  400.                                 if(Len >= Size)
  401.                                 {
  402.                                     if(!StrCmp(&Name[Len - Size],ExecutableSuffix[i] . Name))
  403.                                     {
  404.                                         Type = ExecutableSuffix[i] . Type;
  405.                                         break;
  406.                                     }
  407.                                 }
  408.                             }
  409.  
  410.                             break;
  411.  
  412.                     case TYPE_OBJECT:
  413.                             if(Len >= 4)
  414.                             {
  415.                                 if(!StrCmp(&Name[Len - 4],".LIB"))
  416.                                     Type = TYPE_LIB;
  417.                             }
  418.  
  419.                             break;
  420.  
  421.                     default:    break;
  422.                 }
  423.             }
  424.  
  425.             Close(File);
  426.         }
  427.  
  428.         FreeVec(Buffer);
  429.     }
  430.  
  431.     if(Type != TYPE_FILE && FileTypes[Type])
  432.         SetComment(Name,FileTypes[Type]);
  433.  
  434.     if(Type != TYPE_EXECUTABLE)
  435.         SetProtection(Name,FIBF_EXECUTE);
  436. }
  437.